package top.tenebrous.yzl.chat.controller.system;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import top.tenebrous.yzl.basic.exceptions.BizException;
import top.tenebrous.yzl.basic.model.CtsResult;
import top.tenebrous.yzl.basic.model.YzlChatResult;
import top.tenebrous.yzl.chat.annotation.NoAuthority;
import top.tenebrous.yzl.chat.controller.BaseController;
import top.tenebrous.yzl.chat.mapper.member.MMemUserMapper;
import top.tenebrous.yzl.chat.model.LoginDTO;
import top.tenebrous.yzl.chat.model.LoginResultDto;
import top.tenebrous.yzl.chat.model.MemberLoginInfo;
import top.tenebrous.yzl.chat.service.system.ISysLoginService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Classname controller
 * @Description TODO
 * @Date 2023/11/11 10:59 PM
 * @Created by zhangya
 */
@Slf4j
@RestController
@RequestMapping("/v1/chat")
public class SysLoginController extends BaseController {

  @Autowired
  private MMemUserMapper memUserMapper;

  @Autowired
  private StringRedisTemplate stringRedisTemplate;

  @Resource
  private ISysLoginService loginService;

  /**
   * 账号验证
   *
   * @param loginDTO
   * @return
   */
  @NoAuthority
  @PostMapping("/account/login")
  public YzlChatResult<SaTokenInfo> verifyAccount(@RequestBody LoginDTO loginDTO, HttpServletRequest request) {
    if (StringUtils.isBlank(loginDTO.getPhone())) {
      throw new BizException("手机号不能为空");
    }
    if (StringUtils.isBlank(loginDTO.getPassword())) {
      throw new BizException("密码不能为空");
    }
    return loginService.login(loginDTO);
  }

  @GetMapping("/getLoginInfo")
  @ApiOperation(value = "获取登录后信息")
  public YzlChatResult<LoginResultDto> getLoginInfo() {
    MemberLoginInfo userInfo = getUserInfo();
    return loginService.getLoginInfo(userInfo);
  }

  /**
   * 退出
   *
   * @return
   */
  @PostMapping("/logout")
  public YzlChatResult logout() {
    StpUtil.logout();
    return new YzlChatResult(200, "退出成功。");
  }

}
